package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.entity.FixedAssets;
import com.ky.tms.entity.FixedAssetsLog;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.mapper.FixedAssetsLogMapper;
import com.ky.tms.mapper.FixedAssetsMapper;
import com.ky.tms.service.IFixedAssetsService;
import com.ky.tms.util.RequestContextHolderUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-07-21
 */
@Service
public class FixedAssetsServiceImpl extends ServiceImpl<FixedAssetsMapper, FixedAssets> implements IFixedAssetsService {

    @Autowired
    private FixedAssetsLogMapper fixedAssetsLogMapper;

    @Override
    public IPage<FixedAssets> page(FixedAssets fixedAssets, Long limit, Long pages) {

        QueryWrapper<FixedAssets> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(fixedAssets.getName())){
            queryWrapper.like("name", fixedAssets.getName());
        }
        if(fixedAssets.getType() != null){
            queryWrapper.eq("type", fixedAssets.getType());
        }
        queryWrapper.orderByDesc("id");
        IPage<FixedAssets> page = new Page(pages, limit);
        page = this.page(page, queryWrapper);
        for(FixedAssets fixedAssets1 : page.getRecords()){
            QueryWrapper<FixedAssetsLog> qw = new QueryWrapper<>();
            qw.eq("fixed_assets_id", fixedAssets1.getId());
            qw.orderByDesc("id");
            List<FixedAssetsLog> fixedAssetsLogs = fixedAssetsLogMapper.selectList(qw);
            fixedAssets1.setLogs(fixedAssetsLogs);
        }
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(FixedAssets fixedAssets) throws PredictableException {
        QueryWrapper<FixedAssetsLog> qw = new QueryWrapper<>();
        qw.eq("fixed_assets_id", fixedAssets.getId());
        Integer integer = fixedAssetsLogMapper.selectCount(qw);
        if(integer > 0){
            throw  new PredictableException("已经有申领不能删除！");
        }
        baseMapper.deleteById(fixedAssets);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FixedAssets fixedAssets) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        FixedAssets db = baseMapper.selectById(fixedAssets.getId());
        if(db.getVersion() != fixedAssets.getVersion()){
            throw  new PredictableException("请刷新后重试！");
        }
        if(fixedAssets.getAmount().compareTo(db.getAmount()) != 0){
            QueryWrapper<FixedAssetsLog> qw = new QueryWrapper<>();
            qw.eq("fixed_assets_id", fixedAssets.getId());
            Integer integer = fixedAssetsLogMapper.selectCount(qw);
            if(integer > 0){
                throw  new PredictableException("已经有申领不能改变数量！");
            }
        }
        fixedAssets.setVersion(fixedAssets.getVersion()+1);
        fixedAssets.setUpdateTime(LocalDateTime.now());
        fixedAssets.setUpdateUser(userName);
        baseMapper.updateById(fixedAssets);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void logs(FixedAssetsLog fixedAssetsLog) throws PredictableException {
        FixedAssets db = baseMapper.selectById(fixedAssetsLog.getFixedAssetsId());

        if(db.getVersion() != fixedAssetsLog.getVersion()){
            throw  new PredictableException("请刷新后重试！");
        }
        fixedAssetsLog.setCreateTime(LocalDateTime.now());
        fixedAssetsLog.setCreateUser(RequestContextHolderUtil.getUserName());
        db.setUsedCode(fixedAssetsLog.getChangeUser());
        db.setUsedName(fixedAssetsLog.getChangeUserName());
        db.setVersion(db.getVersion()+1);
        baseMapper.updateById(db);
        fixedAssetsLogMapper.insert(fixedAssetsLog);

    }

    @Override
    public void imgs(FixedAssets fixedAssets) throws PredictableException {
        FixedAssets fixedAssets1 = new FixedAssets();
        fixedAssets1.setId(fixedAssets.getId());
        fixedAssets1.setOkImg(fixedAssets.getOkImg());
        baseMapper.updateById(fixedAssets1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(FixedAssets fixedAssets) throws PredictableException {
        LocalDateTime now = LocalDateTime.now();
        String userName = RequestContextHolderUtil.getUserName();
        fixedAssets.setCreateUser(userName);
        fixedAssets.setCreateTime(now);
        fixedAssets.setIsLock(0);
        fixedAssets.setIsValid(1);
        fixedAssets.setVersion(1l);
        fixedAssets.setComfirmTime(now);
        fixedAssets.setComfirmUser(userName);
        baseMapper.insert(fixedAssets);
    }
}
